$(function(){
	showProjectDetail();
	hideDetail();
});
// 显示详情
function showProjectDetail() {
	var winwidth = $(window).width();
	if (winwidth < 769) {
		$('.tech-content').each(
				function(i) {
					if ($(this).find('.tech-list-extra').length == 0) {
						var type = $(this).find('i.cell.type').text();
						var deal = $(this).find('i.cell.deal').text();
						var $html = $('<div class="tech-list-extra">'
								+ '<p>类型：' + type + '&nbsp;&nbsp;' + '交易方式：'
								+ deal + '</p>' + '</div>');
						$(this).find('div.tech-list-detail').find('div')
								.prepend($html);
					}
				});
		$('.cell.type').css('display', 'none');
		$('.cell.deal').css('display', 'none');
	} else if (winwidth > 768) {
		if ($('.cell.type').css('display') == 'none') {
			$('.cell.type').show();
			$('.cell.deal').show();
			$('.tech-list-extra').remove();
		}
	}
}
// 手机下隐藏详情页
function hideDetail() {
	var browser = {
		versions : function() {
			var u = navigator.userAgent, app = navigator.appVersion;
			return {
				android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,
				iPhone : u.indexOf('iPhone') > -1,
				iPad : u.indexOf('iPad') > -1,
				iPod : u.indexOf('iPod') > -1,
			};
		}(),
		language : (navigator.browserLanguage || navigator.language)
				.toLowerCase()
	}
	if (browser.versions.iPhone || browser.versions.iPad
			|| browser.versions.iPod || browser.versions.android) {
		$('.tech-list-detail').click(function() {
			$(this).css({
				"height" : "0"
			});
		});
	} else {
		$('.tech-list-detail').unbind('click');
	}
}

function getTechList(isPage, page, sort, desc, append ){
	var pageSize = 10;
	var params = null;
	if(!isPage){
		pageSize = 5;
		sort = 'releaseTime';
		desc = 'desc';
	} else {
		params = {origin : origin, industry : industry, category : category, tradetype:tradetype, content:content};
	}
	
	$.ajax({
        type : 'GET',
        url : SERVER_PATH + '/tech/v1/getLists?page=' + page + '&pageSize='+pageSize+'&sort='+sort +'&desc='+desc,
        data : params,
        async:true,
        success : function(res){
            if (res.rescode == 100) {
            	var data = res.data;
            	var $html = '';
            	var $page = '';
            	if(data != null) {
            		if(isPage) {
            			$('#nowpage').val(Number(page) + 1);
            			if(page*pageSize > res.totalSize){
            				$('#getmore').text('已无更多数据');
            				$("#getmore").attr("disabled", true); 
            				$('#getmore').addClass('nodata');
            			} else {
            				$('#getmore').text('加载更多');
            				$("#getmore").attr("disabled", false); 
            				$('#getmore').removeClass('nodata');
            			}
            		}
            		$.each(data, function(i,obj){
            			$html += '<div class="tech-content"><ul class="tech-list-content"><li>';
            			$html += '  <i class="cell project">'
            			$html += '    <a href="' + SERVER_PATH + '/tech/detail?techId='+ obj.tid +'" ><h4>' + obj.name + '</h4></a>';
            			$html += '    <p><i class="iconfont icon-weizhi"></i>' + obj.address + '&nbsp;&nbsp;';
            			$html += '       <i class="iconfont icon-shijian"></i>' + obj.releaseTime + '</p>';
            			if(obj.labels != null || obj.labels.length > 0) {
            				$html += '<p>';
            				$.each(obj.labels, function(li, label){
            					$html += '<a href="#"><span class="project-tag">';
            					$html += label.name + '</span></a>';
            				});
            				$html += '</p>';
            			}
            			$html += '</i>';
            			
            			$html += '	<i class="cell industry">' + (obj.industry != null ? obj.industry.name : '-') + '</i>';
            			$html += '	<i class="cell type">' + (obj.category != null ? obj.category.name : '-') + '</i>';
            			$html += '	<i class="cell deal">' + (obj.tradeType != null ? obj.tradeType.name : '-') + '</i>';
            			$html += '	<i class="cell operation">';
            			$html += '     <a href="javascript:void(0)" onclick="favorite(\''+obj.tid+'\',this)" class="btn"><span id>';
            			$html += (obj.favoriteState == 1 ? '取消关注' : '关注') + '</span></a>';
            			$html += '	</i></li></ul>';
            			$html += '<div class="tech-list-detail"><div title="detail-content">';
            			$html +=  obj.content + '</div></div></div>';
            			
                	});
            	}
            	if(!isPage || !append){
            		$('#techListId').empty();
            	}
            	$('#techListId').append($($html));
            	/*// 展开看详情
            	$('.tech-list-content').click(function() {
            		if(!$(this).hasClass('thead')) {
            			var $selector = $(this).next();
            			if($selector.height() == 0) {
            				$(this).css("border", 0);
            				$(this).parent().css("border-bottom", "1px solid #CCCCCC");
            				$(this).find('li').css("border-bottom", "none");
            				var height = Number($(this).next().find('div').height()) + Number(20);
            				$selector.css("opacity", "1");
            				$selector.css("display", "block");
            				$selector.css("height", height + "px");
            			} else {
            				$selector.css({ "height": "0" });
            			}
            		}
            		return false;
            	});
            	$('.cell>a').click(function(e) {
            		e.stopPropagation();
            	});*/
            }else{
                console.log(data.msg);
            }
        }
	});
}

// 去除空格
function Trim(str) {
    var result;
    result = str.replace(/(^\s+)|(\s+$)/g,"");
    result = result.replace(/\s/g,"");
    return result;
}

// 关注
function favorite(tid, obj){
	$.ajax({
        type : 'post',
        url : SERVER_PATH + '/user/v1/favorite',
        data : {
        	type:1,
        	targetId:tid
        },
        async:false,
        success : function(res){
        	console.log(res);
        	if(res.rescode==100){
        		var text = $(obj).find('span').text();
        		if(text == '关注') {
            		$(obj).find('span').text('取消关注');
        		} else {
            		$(obj).find('span').text('关注');
        			
        		}
	
        	} else if( res.rescode == 101) {
        		window.location.href=SERVER_PATH + '/login';
        	} else {
        		alert("操作失败" + res.msg);
        	}
        }
	});
}